package com.yilin.platform.base_core.commons.utils;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2018/12/7.
 */

public class CommonUtil {

    /**
     * 检查是否车牌号
     */
    public static boolean checkCarNumber(String content) {
        String pattern = "([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼]{1}(([A-HJ-Z]{1}[A-HJ-NP-Z0-9]{5})|([A-HJ-Z]{1}(([DF]{1}[A-HJ-NP-Z0-9]{1}[0-9]{4})|([0-9]{5}[DF]{1})))|([A-HJ-Z]{1}[A-D0-9]{1}[0-9]{3}警)))|([0-9]{6}使)|((([沪粤川云桂鄂陕蒙藏黑辽渝]{1}A)|鲁B|闽D|蒙E|蒙H)[0-9]{4}领)|(WJ[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼·•]{1}[0-9]{4}[TDSHBXJ0-9]{1})|([VKHBSLJNGCE]{1}[A-DJ-PR-TVY]{1}[0-9]{5})";
        return Pattern.matches(pattern, content);
    }

    /**
     * 格式化double为保留两位小数的字符串
     */
    public static String formatDoubleTwo(Double value) {
        try {
            if (value == null) {
                value = 0d;
            }
            DecimalFormat df = new DecimalFormat("######0.00");
            return df.format(value);
        } catch (Exception e) {
            return String.valueOf(value);
        }
    }

    public static String formatDoubleThree(Double value) {
        try {
            if (value == null) {
                value = 0d;
            }
            DecimalFormat df = new DecimalFormat("######0.000");
            return df.format(value);
        } catch (Exception e) {
            return String.valueOf(value);
        }
    }

    public static String trimNumber(String floatString) {
        if (floatString.indexOf(".") > 0) {
            //正则表达
            floatString = floatString.replaceAll("0+?$", "");//去掉后面无用的零
            floatString = floatString.replaceAll("[.]$", "");//如小数点后面全是零则去掉小数点
        }
        return floatString;
    }


    /**
     * 基準計算去尾
     * 7、 HAIL_EVEN：银行家舍入法。
     */
    public static Double doubleThree(Double value) {
        try {
            if (value == null) {
                value = 0d;
            }
            BigDecimal d = new BigDecimal(value);
            BigDecimal newValue = d.setScale(3, RoundingMode.HALF_EVEN);     //使用银行家算法保留3為
            return newValue.doubleValue();
        } catch (Exception e) {
            return value;
        }
    }

    public static Double doubleTwo(Double value) {
        try {
            if (value == null) {
                value = 0.0d;
            }
            BigDecimal d = new BigDecimal(value);
            BigDecimal newValue = d.setScale(2, RoundingMode.HALF_EVEN);     //使用银行家算法保留2為
            return newValue.doubleValue();
        } catch (Exception e) {
            return value;
        }
    }


    public static Integer intValue(Double value) {
        try {
            if (value == null) value = 0d;
            BigDecimal d = new BigDecimal(value);
            BigDecimal newValue = d.setScale(0, RoundingMode.HALF_UP);     //使用银行家算法保留2為
            return newValue.intValue();
        } catch (Exception e) {
            return value.intValue();
        }
    }

    /**
     * 获取服务器ip和端口号
     */
    public static String getUrl() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) return "";
        HttpServletRequest request = requestAttributes.getRequest();
        String localAddr = request.getLocalAddr();
        int serverPort = request.getServerPort();
        return "http://" + localAddr + ":" + serverPort;
    }
}
